package cn.sciento.iam.api.controller.v1;

import cn.sciento.core.base.BaseConstants;
import cn.sciento.core.exception.CommonException;
import cn.sciento.core.iam.ResourceLevel;
import cn.sciento.core.util.Results;
import cn.sciento.core.util.ValidUtils;
import cn.sciento.iam.api.dto.SubModuleDTO;
import cn.sciento.iam.app.service.SubModuleService;
import cn.sciento.iam.config.SwaggerApiConfig;
import cn.sciento.iam.domain.entity.SubModule;
import cn.sciento.iam.domain.repository.SubModuleRepository;
import cn.sciento.mybatis.helper.SecurityTokenHelper;
import cn.sciento.starter.keyencrypt.core.Encrypt;
import cn.sciento.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.ValidationUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Validation;
import javax.validation.Validator;
import java.util.List;

@Api(tags = SwaggerApiConfig.IAM_SUB_MODULE_SITE)
@RestController("subModuleSiteController.v1")
@RequestMapping("/v1/sub-module")
public class SubModuleSiteController {

    @Autowired
    private SubModuleService subModuleService;

    @Autowired
    private SubModuleRepository subModuleRepository;
    @Autowired
    private Validator validator;
    
    @ApiOperation("子模块-添加子模块")
    @Permission(level = ResourceLevel.SITE)
    @PostMapping(value = "")
    public ResponseEntity<SubModule> create(@RequestBody SubModule subModule){
        ValidUtils.valid(validator,subModule);

        subModuleRepository.insert(subModule);
        return Results.success(subModule);
    }

    @ApiOperation("子模块-更新子模块")
    @Permission(level = ResourceLevel.SITE)
    @PutMapping(value = "")
    public ResponseEntity<SubModule> update(@RequestBody SubModule subModule){
        ValidUtils.valid(validator,subModule);
        subModuleRepository.updateByPrimaryKeySelective(subModule);
        return Results.success(subModule);
    }


    @ApiOperation("子模块-获取所有模块")
    @Permission(level = ResourceLevel.SITE)
    @GetMapping(value = "")
    public ResponseEntity<List<SubModule>> list(SubModuleDTO subModuleDTO){
        subModuleDTO.setTenantId(BaseConstants.DEFAULT_TENANT_ID);
        return Results.success(subModuleService.listSubModule(subModuleDTO));
    }

    @ApiOperation("子模块-通过id获取模块信息")
    @Permission(level = ResourceLevel.SITE)
    @GetMapping(value = "/{id}")
    public ResponseEntity<SubModule> get(@PathVariable Long id){
        return Results.success(subModuleRepository.selectByPrimaryKey(id));
    }



    @ApiOperation("子模块-禁用子模块")
    @Permission(level = ResourceLevel.SITE)
    @PutMapping(value = "/disable")
    public ResponseEntity<Void> disable(@RequestBody @Encrypt SubModule subModule){
        SecurityTokenHelper.validToken(subModule);
        subModuleService.disable(subModule);
        return Results.success();
    }

    @ApiOperation("子模块-启用子模块")
    @Permission(level = ResourceLevel.SITE)
    @PutMapping(value = "/enable")
    public ResponseEntity<Void> enable(@RequestBody @Encrypt SubModule subModule){
        SecurityTokenHelper.validToken(subModule);
        subModuleService.enable(subModule);
        return Results.success();
    }

    @ApiOperation("子模块-删除子模块")
    @Permission(level = ResourceLevel.SITE)
    @DeleteMapping(value = "")
    public ResponseEntity<Void> delete(@RequestBody @Encrypt SubModule subModule){
        SecurityTokenHelper.validToken(subModule);
        subModuleService.delete(subModule);
        return Results.success();
    }

    @ApiOperation("子模块-获取租户的子模块")
    @GetMapping("/tenant")
    @Permission(level = ResourceLevel.SITE)
    public ResponseEntity<List<SubModule>> getTenantModule(SubModuleDTO subModuleDTO){
        if(subModuleDTO.getTenantId() == null){
            throw new CommonException("hiam.error.tenant.notFound");
        }
        List<SubModule> subModuleList = subModuleService.listSubModule(subModuleDTO);
        return Results.success(subModuleList);
    }

    @ApiOperation("子模块-给租户分配模块")
    @PostMapping("/assign")
    @Permission(level = ResourceLevel.SITE)
    public ResponseEntity<Void> assign(Long tenantId,@RequestBody List<SubModule> subModules){
        SecurityTokenHelper.validToken(subModules);
        subModuleService.assignTenant(tenantId,subModules);
        return Results.success();
    }




}
